//
//  WPNickNameVC.m
//  wobingwoyi
//
//  Created by WGP on 16/8/3.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNickNameVC.h"

@interface WPNickNameVC ()

@property (weak, nonatomic) IBOutlet UITextField *nickNameField;


@end

@implementation WPNickNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [btn sizeToFit];
    [btn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:btn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    /**
     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacer.width = -8;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_left, nil];
}


- (void)backBtnDidClick {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveBtnDidClick:(UIButton *)sender {
    
//    取消键盘
    [self.view endEditing:YES];
    
    if (!self.nickNameField.text.length)
    {
        [self showErrorInfoWithStatus:@"昵称不能为空"];
        return ;
    }
    
    if(self.nickNameField.text.length > 10)
    {
        [self showErrorInfoWithStatus:@"昵称超出限制长度"];
        return ;
    }
    
    WPUserAccount *userAccount = [WPUserAccountViewModel sharedInstance].userAccount;
    userAccount.user.name = self.nickNameField.text;
    
    self.nickNameLabel.text = self.nickNameField.text;
    
    
    NSString *url = @"user/updateUser.do";
    NSString *JSONStr = [userAccount.user yy_modelToJSONString];
    NSDictionary *dict = @{@"json":JSONStr};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = (NSDictionary *)responseObject;
        if ([dict[@"result"] isEqualToString:@"success"])
        {
            WPUserAccount *userAccount = [WPUserAccountViewModel sharedInstance].userAccount;
            [userAccount saveUserAccount];
            userAccount = nil;
            
            [self backBtnDidClick];
            [self showSuccessInfoWithStatus:@"设置成功"];
        }
        else
        {
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //                账号过期
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else
            {
                [self showErrorInfoWithStatus:@"更新失败"];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
        
    }];
}

@end
